#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mqtt.h"
#include "server.h"
#define ILL_OVER_THROUGH      8
#define ILL_UNDER_THROUGH     3
#define TEMP_OVER_THROUGH     25
#define TEMP_UNDER_THROUGH    15
#define CO2_OVER_THROUGH      300
#define CO2_UNDER_THROUGH     50
#define PM_OVER_THROUGH       100
#define PM_UNDER_THROUGH      30

#define PUB_TOPIC "FS_VRJJ/control"
#define LAMP_ON    "{\"lamp\":true}"
#define LAMP_OFF   "{\"lamp\":false}"
#define FAN_OFF    "{\"fan\":false}"
#define FAN_ON     "{\"fan\":true}"
#define SUNSHADE_FORDWARD "{'sunshade':forward}"
#define SUNSHADE_REVERSE  "{'sunshade':reverse}"
#define SUNSHADE_STOP     "{'sunshade':stop}"
#define ALARM_OFF         "{\"alarm\":false}"
#define ALARM_ON          "{\"alarm\":true}"
#define LIGHT      "{\"light\":%f}"
#define TEMP       "{\"tem\":%f}"
#define CO2        "{\"co2\":%f}"
#define PM         "{\"PM2.5\":%f}"
#define HUM        "{\"hum\":%f}"

void check_ill(float ill)
{
	if(ill >= ILL_OVER_THROUGH)
	{
		mqtt_publish(CTRL_PUB_TOPIC, LAMP_OFF);
	}
	else if(ill <= ILL_UNDER_THROUGH)
	{
		mqtt_publish(CTRL_PUB_TOPIC, LAMP_ON);
	}
	else
	{	
	}
	printf("alarm ill value:%f \n", ill);
}

void check_temp(float temp)
{
	if(temp >= TEMP_OVER_THROUGH)
	{
		mqtt_publish(CTRL_PUB_TOPIC, SUNSHADE_FORDWARD);
		sleep(3);
		mqtt_publish(CTRL_PUB_TOPIC, SUNSHADE_STOP);
	}
	else if(temp <= TEMP_UNDER_THROUGH)
	{
		mqtt_publish(CTRL_PUB_TOPIC, SUNSHADE_REVERSE);
		sleep(3);
		mqtt_publish(CTRL_PUB_TOPIC, SUNSHADE_STOP);
	}
	else
	{	
	}
	printf("alarm temp value:%f \n", temp);
}

void check_co2(float co2)
{
	if(co2 >= CO2_OVER_THROUGH)
	{
		mqtt_publish(CTRL_PUB_TOPIC, ALARM_ON);
	}
    sleep(3);
    mqtt_publish(CTRL_PUB_TOPIC, ALARM_OFF);

	printf("alarm co2 value:%f \n", co2);
}

void check_pm(float pm)
{
	if(pm >= PM_OVER_THROUGH)
	{
		mqtt_publish(CTRL_PUB_TOPIC, FAN_ON);
	}
	else if(pm <= PM_UNDER_THROUGH)
	{
		mqtt_publish(CTRL_PUB_TOPIC, FAN_OFF);
	}
	else
	{	
	}
	printf("alarm pm value:%f \n", pm);
}


int main(int argc, char *argv[])
{
	int ret;
	float ill, temp, co2, pm, hum;
	char data[1024] = {0};
	node_t qt_data;

    //初始化服务器
    ret = init_network(atoi(argv[1]));
    if(ret < 0)
	{
        puts("init_server err");
        return -1;
    }

	if (0 != init_mqtt())
	{
		puts("init_mqtt err");
		return -1;
	}else{
		puts("init_mqtt success");
	}

	while(1)
	{
		ill = rand()%10;
		memset(data, 0, sizeof(data));
		sprintf(data, LIGHT, ill);
		mqtt_publish(PUB_TOPIC, data);
		
		memset(&qt_data, 0, CONTROL_CMD_LEN);
		qt_data.val.f_val = ill;
        qt_data.key = CLIENT_CMD_KEY_ILL;
		send_data(qt_data);
		
		temp =rand()%35;
		memset(data, 0, sizeof(data));
		sprintf(data, TEMP, temp);
		mqtt_publish(PUB_TOPIC, data);
		
		memset(&qt_data, 0, CONTROL_CMD_LEN);
		qt_data.val.f_val = temp;
        qt_data.key = CLIENT_CMD_KEY_AIR;
		strcpy(qt_data.name, "tem");
		send_data(qt_data);
		
		hum =rand()%20;
		memset(data, 0, sizeof(data));
		sprintf(data, HUM, hum);
	//	mqtt_publish(PUB_TOPIC, data);
		
		memset(&qt_data, 0, CONTROL_CMD_LEN);
		qt_data.val.f_val = hum;
        qt_data.key = CLIENT_CMD_KEY_AIR;
		strcpy(qt_data.name, "hum");
		send_data(qt_data);
		
	    co2 = rand()%500;
		memset(data, 0, sizeof(data));
		sprintf(data, CO2, co2);
		mqtt_publish(PUB_TOPIC, data);

        memset(&qt_data, 0, CONTROL_CMD_LEN);
        qt_data.val.f_val = co2;
        qt_data.key = CLIENT_CMD_KEY_CO2;
		send_data(qt_data);		
		
		pm = rand()%200;
		memset(data, 0, sizeof(data));
		sprintf(data, PM, pm);
		mqtt_publish(PUB_TOPIC, data);
		
		memset(&qt_data, 0, CONTROL_CMD_LEN);
		qt_data.val.f_val = pm;
        qt_data.key = CLIENT_CMD_KEY_PM;
		send_data(qt_data);	
		
		if(!get_client_ctrl())
		{
		    check_ill(ill);
		    check_temp(temp);
		    check_co2(co2);
		    check_pm(pm);
		}
	}

	exit_mqtt();
	// 7关闭连接--close
    close_network();

	return 0;
}
